<?php
namespace app\common\logic;
use fast\Http;
use think\Cache;
use think\Db;
use think\exception\ValidateException;

/**
 * Created by PhpStorm.
 * User: yangchunfu
 * Date: 2019/12/24
 * Time: 4:36 PM
 */

class HandlerApi
{
    protected $searchUrl = '';
    static $cacheTime = 5 * 60;  //缓存5分钟
    public function __construct()
    {
        $this->searchUrl = config('site.search_url');
    }

    public function search($keyword, $page) {
        $cache_name = MD5($keyword . '_' . $page); //缓存标签
        if ($page === 1) {
            $ad_1 = AdHandler::create(['mark'=> 'search', 'rule'=> 1])->select(['keyword' => ['like', "%$keyword%"]]);  //第一个广告位置
            $ad_2 = AdHandler::create(['mark'=> 'search', 'rule'=> 2])->select(['keyword' => ['like', "%$keyword%"]]);  //第二个广告位置
            $ad_3 = AdHandler::create(['mark'=> 'search', 'rule'=> 3])->select(['keyword' => ['like', "%$keyword%"]]);  //第三个个广告位置

            $ads = [
                $ad_1,
                $ad_2,
                $ad_3
            ];
        } else {
            $ad_other = AdHandler::create(['mark'=> 'search', 'rule'=> 'other'])->select(['keyword' => ['like', "%$keyword%"]]);  //其他广告位置
            $ads['ad_other'] = $ad_other;
        }
        //$cache_data = Cache::get($cache_name);
        $cache_data = false;
        if ($cache_data) {
            $return_data = $cache_data;
        } else {
            //查询远程新闻
            $res = Http::get($this->searchUrl, ['keyword' => $keyword, 'page'=> $page]);
            $res = json_decode($res, true);

            if (!$res) {
                throw new ValidateException('未搜索到内容');
            }
            if ($res['success'] == false) {
                throw new ValidateException($res['errorMsg']);

            }

            $return_data = [];
            if (isset($res['single'])) {
                foreach ($res['single'] as $k => $v) {
                    $return_data = array_merge($return_data, $v);
                }
            }

           // Cache::tag('__SEARCH_NEWS__')->set($cache_name, $return_data, self::$cacheTime);
        }


        return [
            'ads' => $ads,
            'news' => $return_data
        ];

    }


    /**
     * 首页接口
     * @param int $page
     */
    public function first($page = 1) {
        $ad_1_5 = [];
        $hot = [];
        //首页1-5广告
        if ($page == 1) {
            $ad_1_5 = AdHandler::create(['mark'=> 'first', 'rule'=> '1_5'])->select();
            $hot = Db::name('self_news')->whereTime('createtime','-7 day')->order('viewCount desc')->limit(4)->select();
            if (!$hot) {
                $hot = Db::name('self_news')->order('viewCount desc')->limit(4)->select();
            }
        }

        $other = AdHandler::create(['mark'=> 'first', 'rule'=> 'other'])->select();

        return [
            'ad_1_5' => $ad_1_5,
            'hot' => $hot,
            'other'  => $other
        ];
    }

    /**启动广告*/
    public function start() {
        $start = AdHandler::create(['mark'=> 'start', 'rule'=> '1'])->select();

        return $start;
    }



}